package com.right.near.common.filter;

import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author star
 * @version 1.0.0
 * @Description: 认证拦截
 * @date 2020/7/20
 */
@Component
@Slf4j
public class AuthTokenFilter extends OncePerRequestFilter {

//    @Autowired
//    private RedisTemplate redisTemplate;
//    @Autowired
//    private CurrentUserUtil currentUserUtil;

    @Override
    protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull FilterChain filterChain) throws ServletException, IOException {
//
//        // 获取token
//        String token = currentUserUtil.getToken(request);
//
//        // 验证token
//        if (StringUtils.hasText(token)) {
//            ValueOperations<String, UserDetail> valueOperations = redisTemplate.opsForValue();
//            UserDetail userDetail = valueOperations.get(RedisKeyConstants.AUTH_TOKEN.concat(token));
//            if (null != userDetail && null != userDetail.getId()) {
//                UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetail, null, userDetail.getAuthorities());
//                authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                logger.info(String.format("Authenticated userDetail %s, setting security context", userDetail.getAccount()));
//                SecurityContextHolder.getContext().setAuthentication(authentication);
//                // token延长时间
//                valueOperations.set(token, userDetail, NumberConstants.FOUR_LONG, TimeUnit.HOURS);
//            }
//        }
        filterChain.doFilter(request, response);
    }

}
